from datetime import datetime
from decimal import Decimal
from typing import Optional

from pydantic import BaseModel, Field

from app.models.booking import BookingStatus


class BookingBase(BaseModel):
    passenger_id: int = Field(..., gt=0, description="乘客ID，必须是大于0的整数，用于关联乘客信息")
    flight_id: int = Field(..., gt=0, description="航班ID，必须是大于0的整数，用于关联航班信息")
    seat_number: Optional[str] = Field(None, max_length=10, description="座位号，可选字段，最多10个字符")
    price: Decimal = Field(..., gt=0, description="预订价格，必须是大于0的数值，表示该预订的金额")


class BookingCreate(BookingBase):
    """创建预订请求的数据模型"""
    pass


class BookingUpdate(BaseModel):
    """更新预订信息的数据模型"""
    seat_number: Optional[str] = Field(None, max_length=10, description="座位号，可选字段，最多10个字符")
    booking_status: Optional[BookingStatus] = Field(None,
                                                    description="预订状态，可选字段，包括pending, confirmed, cancelled")


class BookingResponse(BaseModel):
    """预订信息响应的数据模型"""
    id: int = Field(..., description="预订记录的唯一标识符")
    booking_number: str = Field(..., description="预订编号，唯一标识一次预订")
    passenger_id: int = Field(..., description="乘客ID，关联乘客信息")
    flight_id: int = Field(..., description="航班ID，关联航班信息")
    seat_number: Optional[str] = Field(None, description="座位号")
    booking_status: BookingStatus = Field(..., description="预订状态，包括pending, confirmed, cancelled")
    booking_date: datetime = Field(..., description="预订日期时间")
    price: Decimal = Field(..., description="预订价格")
    created_at: datetime = Field(..., description="记录创建时间")
    updated_at: Optional[datetime] = Field(None, description="记录更新时间")

    class Config:
        from_attributes = True


class BookingWithDetails(BaseModel):
    """包含详细信息的预订数据模型"""
    id: int = Field(..., description="预订记录的唯一标识符")
    booking_number: str = Field(..., description="预订编号，唯一标识一次预订")
    passenger: dict = Field(..., description="乘客详细信息")
    flight: dict = Field(..., description="航班详细信息")
    seat_number: Optional[str] = Field(None, description="座位号")
    booking_status: BookingStatus = Field(..., description="预订状态，包括pending, confirmed, cancelled")
    booking_date: datetime = Field(..., description="预订日期时间")
    price: Decimal = Field(..., description="预订价格")

    class Config:
        from_attributes = True


class SeatSelection(BaseModel):
    """座位选择的数据模型"""
    seat_number: str = Field(..., max_length=10, description="座位号，必须字段，最多10个字符")


class BookingCancellation(BaseModel):
    """预订取消的数据模型"""
    message: str = Field(..., description="取消操作的结果消息")
    booking: dict = Field(..., description="被取消的预订详细信息")
